'use strict';
layui.define(['form', 'layer'], function (exports) {
    var layer = layui.layer,
        form = layui.form();

    $(".layui-canvs")
        .width($(window).width())
        .height($(window).height())
        .jParticle({
        background: "#141414",
        color: "#E6E6E6"
    });

    //登陆验证
    form.verify({
        username: function(value){
            if(value==""){
                return '用户名不能为空';
            }
            if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                $('.verifyImg').attr('src',"getCode?"+new Date().getTime());
                return '用户名不能有特殊字符';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                $('.verifyImg').attr('src',"getCode?"+new Date().getTime());
                return '用户名首尾不能出现下划线\'_\'';
            }
            if(/^\d+\d+\d$/.test(value)){
                $('.verifyImg').attr('src',"getCode?"+new Date().getTime());
                return '用户名不能全为数字';
            }
        }
        ,password: function(value){
            if(value==""){
                return '密码不能为空';
            }
            if(!/^[\S]{5,12}$/.test(value)){
                $('.verifyImg').attr('src',"getCode?"+new Date().getTime());
                return '密码必须5到12位，且不能出现空格';
            }
        }
    });

    form.on('submit(*)', function (data) {
        $.ajax({
            url: '/login',
            type: 'post',
            data: data.field,
            dataType:'json',
            success: function (info) {
                if (info.errno === 0) {
                    layer.load(1, {
                        shade: [0.1,'#fff']
                    });
                    location.href='/admin/index'
                }else{
                    layer.msg(info.msg,{icon: 5,shift: 6});
                }
            }
        });

        return false;
    });
});